/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eproject2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hoang
 */
public class SQLConnects implements Serializable {

    String username;
    String password;
    String server;
    String port;
    String database;
    String Error;

    public SQLConnects(String username, String password, String server, String port, String database) {
        this.username = username;
        this.password = password;
        this.server = server;
        this.port = port;
        this.database = database;
    }

    public SQLConnects() {
        this.username = "sa";
        this.password = "";
        this.server = "localhost";
        this.port = "1433";
        this.database = "ContactManager";
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public SQLConnects getinfo() {
        try {
            FileInputStream fis = null;
            ObjectInputStream ois = null;
            String filename = "D:\\setting.dat";
            fis = new FileInputStream(filename);
            ois = new ObjectInputStream(fis);
            SQLConnects temp = (SQLConnects) ois.readObject();
            return temp;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SQLConnects.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(SQLConnects.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public boolean testconnect() {
        try {
            java.sql.Connection conn;
            String urlString = "jdbc:sqlserver://" + this.server + ":" + this.port + ";DatabaseName=" + this.database + ";User=" + this.username + ";Password=" + this.password + ";";
            String driverString = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            Class.forName(driverString);
            conn = DriverManager.getConnection(urlString);
            conn.close();
            return true;
        } catch (SQLException ex) {
//            Logger.getLogger(SQLConnects.class.getName()).log(Level.SEVERE, null, ex);
            Error = ex.getLocalizedMessage();
            //"Cannot open database "ContactManager" requested by the login. The login failed."
            return false;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SQLConnects.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean checksetting() {
        File f = new File("D:\\setting.dat");
        if (f.exists()) {
            FileInputStream fis = null;
            try {
                ObjectInputStream ois = null;
                String filename = "D:\\setting.dat";
                fis = new FileInputStream(filename);
                ois = new ObjectInputStream(fis);
                SQLConnects temp = (SQLConnects) ois.readObject();
                if (temp.testconnect()) {
                    return true;
                } else {
                    return false;
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SQLConnects.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            } catch (IOException ex) {
                Logger.getLogger(SQLConnects.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        } else {
            return false;
        }
    }
}
